package com.zzyl.serve.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;


    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Autowired

    private RedisTemplate<Object,Object> redisTemplate;

    @Autowired
    private static final String CACHE_KEY_PREFIX = "nursingPlan:all";
    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        //创建个vo
        NursingPlanVo nursingPlanVo = new NursingPlanVo();


        NursingPlan nursingPlan = getById(id);


        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);

        //根据Planid找项目
        List<NursingProjectPlanVo> projectPlans = nursingProjectPlanMapper.selectByPlanId(id);
        nursingPlanVo.setProjectPlans(projectPlans);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {

        //保存nursingPlan
        //保存 相关的项目
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        boolean save = save(nursingPlan);


        if(save){
            //redis
            deleteCacheById();

            List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
            for (NursingProjectPlan nursingProjectPlan : projectPlans) {
                nursingProjectPlan.setPlanId(nursingPlan.getId());
            }

            nursingProjectPlanMapper.batchInsert(projectPlans);
        }


        return save ? 1 : 0;
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */

    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //拷贝到属性
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);

        //判断项目列表是否为空
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();

        if(!CollectionUtils.isEmpty(projectPlans)){
            projectPlans.forEach(nursingProjectPlan -> nursingProjectPlan.setPlanId(nursingPlan.getId()));

            nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());
            nursingProjectPlanMapper.batchInsert(projectPlans);
        }

        //为空直接修改 不为空 删除全部计划 再批量新增


        //redis
        boolean b = updateById(nursingPlan);
        if (b){
            deleteCacheById();
        }
        return b ? 1 : 0;
    }

    /**
     * 批量删除护理计划
     * 
     * @param id 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNursingPlanByIds(Long id)
    {
        //删除 plan_id的项目
        nursingProjectPlanMapper.deleteByPlanId(id);

        //redis
        boolean b = removeById(id);
        if (b){
            deleteCacheById();
        }
        //删除id的计划
        return b ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {

        boolean b = removeById(id);
        if (b){
            deleteCacheById();
        }
        return b ? 1 : 0;

    }

    @Override
    public List<NursingPlan> listAll() {
        //如果redis有， 直接就返回 如果没有就 查询数据库 再保存到redis

        List<NursingPlan> nursingPlans = (List<NursingPlan>)redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);

        if(!CollectionUtils.isEmpty(nursingPlans)){
            return nursingPlans;
        }

        LambdaQueryWrapper<NursingPlan> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(NursingPlan::getStatus, 1);
        nursingPlans = list(queryWrapper);

        if(!CollectionUtils.isEmpty(nursingPlans)){

            redisTemplate.opsForValue().set(CACHE_KEY_PREFIX, nursingPlans);
        }

        return nursingPlans;
    }

    public void deleteCacheById(){

        redisTemplate.delete(CACHE_KEY_PREFIX);
    }
}
